/**
 菜单 处理
 **/
(function (vc) {
    var vm = new Vue({
        el: '#menu-nav',
        data: {
            menus: [],
            logo: '',
            nav: {
                _currentCommunity: '',
                communityInfos: [],
                _currentStore: '',
                storeInfos: []
            }
        },
        created:function(){
                console.log('移动端打开',vc.queryUrlParams('flag'))
                if(vc.queryUrlParams('flag')=='wechat'){
                    $('#menu-nav,#header_main,#header_title,#copyright').hide();
                    $('#vc-page').css({
                        marginLeft:"0px"
                    })
                }else{
                    $('#menu-nav,#header_main,#header_title,#copyright').show();
                    $('#vc-page').css({
                        marginLeft:"220px"
                    })
                    //pc端进入这里
                    console.log('展示侧边栏')
                }
            //微信端过来，强制刷新
            if( vc.queryUrlParams('flag')=='wechat' ){
               // this.getNavStore(1,3)
            }
        },
        mounted: function () {
            this._initSysInfo();
            this.getMenus();
            let _menuDiv = document.getElementById('menu-nav');
            vc.eleResize.on(_menuDiv, function () {
                //console.log('resize', '大小修改了');
                vc._fix_height(_menuDiv);
            });
            window.onscroll = function () {
                //为了保证兼容性，这里取两个值，哪个有值取哪一个
                //scrollTop就是触发滚轮事件时滚轮的高度
                let _menuDivb = document.getElementById('menu-nav');
                vc._fix_height(_menuDivb);
            }
        },
        methods: {
            _initSysInfo: function () {
                var sysInfo = vc.getData("_sysInfo");
                if (sysInfo == null) {
                    this.logo = "CH";
                    return;
                }
                this.logo = sysInfo.logo;
            },
            _gotoIndex: function () {
                vc.jumpToPage("/")
            },
            getMenus: function () {

                var _tmpMenus = vc.getMenus();
                //浏览器缓存中能获取到
                if (_tmpMenus != null && _tmpMenus != undefined) {
                    this.miniMenu();
                    this.menus = _tmpMenus;
                    // 为每一个子菜单添加一个active属性
                    for (var i=0;i<this.menus.length;i++) {
                        if(this.menus[i].childs){
                            this.menus[i].childs.forEach(item=>{
                                this.$set(item,'active',false)
                            })
                        }
                    }
                    console.log(this.menus)
                    return;
                }

                var param = {
                    params: {
                        msg: this.message
                    }

                }
                //发送get请求
                vc.http.get('menu','getMenus',
                    param,
                    function (json, res) {
                        var _menus = JSON.parse(json);
                        if (_menus == null || _menus.length == 0) {
                            return;
                        }
                        _menus.sort(function (a, b) {
                            return a.seq - b.seq
                        });
                        var _currentMenusId = vc.getCurrentMenu() == null ? _menus[0].id : vc.getCurrentMenu();
                        vm.menus = vm.refreshMenuActive(_menus, _currentMenusId);
                        vc.setMenus(vm.menus);
                        vm.miniMenu();
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            refreshMenuActive: function (jsonArray, _id) {
                for (var menuIndex = 0; menuIndex < jsonArray.length; menuIndex++) {

                    if (jsonArray[menuIndex].hasOwnProperty('childs')) {
                        var _childs = jsonArray[menuIndex].childs;
                        _childs.sort(function (_child, _newChild) {
                            return _child.seq - _newChild.seq
                        });
                        jsonArray[menuIndex].childs = _childs;
                    }

                    if (_id === jsonArray[menuIndex].id) {
                        if (jsonArray[menuIndex].active === true) {
                            //如果当前本身是打开状态，说明 需要关闭
                            jsonArray[menuIndex].active = false;
                            continue;
                        }
                        jsonArray[menuIndex].active = true;
                        continue;
                    }
                    jsonArray[menuIndex].active = false;
                }

                return jsonArray;
            },
            switchMenu: function (_id) {
                //设置菜单ID
                vc.setCurrentMenu(_id);
                vm.menus = vm.refreshMenuActive(vm.menus, _id);
                vc.setMenus(vm.menus);
                //vc._fix_height()
            },
            miniMenu: function () {

                //菜单默认为打开方式
                if (!vc.notNull(vc.getMenuState())) {
                    vc.setMenuState('ON');
                }

                if (vc.notNull(vc.getMenuState()) && vc.getMenuState() == 'ON') {
                    return;
                }

                $("body").toggleClass("mini-navbar");
                vc.setMenuState('OFF');
            },
            _gotoPage: function (_href) {
				var newStr=_href.indexOf("http");
				if(newStr == 0){
                    if(_href.indexOf("?")>0){
						_href += "&token="+cookie;
                    }else {
						_href += "?token="+cookie;
                    }
					window.open(_href)
                }else {
					vc.jumpToPage(_href);
                }

            },
            addActive(jsonArray,_id){
                for (var menuIndex = 0; menuIndex < jsonArray.length; menuIndex++) {
                    if (_id === jsonArray[menuIndex].href) {
                        console.log(jsonArray[menuIndex].active)
                        if (jsonArray[menuIndex].active === true) {
                            //如果当前本身是打开状态，说明 需要关闭
                            jsonArray[menuIndex].active = false;
                            continue;
                        }
                        jsonArray[menuIndex].active = true;
                        continue;
                    }
                    jsonArray[menuIndex].active = false;
                }
                return jsonArray;
            },
            browserVersions(){
                var ua = navigator.userAgent; /* navigator.userAgent 浏览器发送的用户代理标题 */
                if (ua.indexOf('Android') > -1 || ua.indexOf('iPhone') > -1 || 
                    ua.indexOf('iPad') > -1 || ua.indexOf('Symbian') > -1 ||
                    ua.indexOf('BlackBerry') > -1){
                    return true;　
                }else{
                    return false;
            　　}
            },
            queryUrlParams(name){
                // //拼接正则表达式
                // var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                // //匹配参数
                // var r = window.location.search.substr(1).match(reg);
                // //获取参数
                // if (r != null) return decodeURIComponent(r[2]);
                // return null;
                var reg = new RegExp("(^|\&\?)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.href.substr(1).match(reg);
                if (r != null) {
                    return unescape(r[2]);
                } else {
                    return null;
                }
            }

        },

    });

})(window.vc)
